package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.JobFavoriteDTO;
import com.example.joblinker.dto.ResumeViewDTO;
import com.example.joblinker.dto.UserTodoDTO;
import com.example.joblinker.service.PersonalService;
import com.example.joblinker.util.JwtUtil;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 个人中心控制器
 */
@RestController
@RequestMapping("/user/personal")
public class PersonalController {

    @Resource
    private PersonalService personalService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 获取用户统计数据
     */
    @GetMapping("/stats")
    public Result<Map<String, Integer>> getUserStats(HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Map<String, Integer> stats = personalService.getUserStats(userId);
        return Result.success(stats);
    }

    /**
     * 获取用户待办事项
     */
    @GetMapping("/todos")
    public Result<List<UserTodoDTO>> getUserTodos(
            HttpServletRequest request,
            @RequestParam(required = false) Integer limit) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        List<UserTodoDTO> todos = personalService.getUserTodos(userId, limit);
        return Result.success(todos);
    }

    /**
     * 获取推荐职位
     */
    @GetMapping("/recommend-jobs")
    public Result<Page<Map<String, Object>>> getRecommendJobs(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<Map<String, Object>> jobs = personalService.getRecommendJobs(userId, page, size);
        return Result.success(jobs);
    }

    /**
     * 获取收藏职位列表
     */
    @GetMapping("/favorites")
    public Result<Page<JobFavoriteDTO>> getFavoriteJobs(
            HttpServletRequest request,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "12") Integer size) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<JobFavoriteDTO> favorites = personalService.getFavoriteJobs(userId, keyword, page, size);
        return Result.success(favorites);
    }

    /**
     * 添加职位收藏
     */
    @PostMapping("/favorites")
    public Result<Void> addFavoriteJob(
            HttpServletRequest request,
            @RequestBody JobIdWrapper jobIdWrapper) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        boolean result = personalService.addFavoriteJob(userId, jobIdWrapper.getJobId());
        return result ? Result.success() : Result.error("收藏失败");
    }

    /**
     * 取消职位收藏
     */
    @DeleteMapping("/favorites/{jobId}")
    public Result<Void> removeFavoriteJob(
            HttpServletRequest request,
            @PathVariable Long jobId) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        boolean result = personalService.removeFavoriteJob(userId, jobId);
        return result ? Result.success() : Result.error("取消收藏失败");
    }

    /**
     * 获取简历查看记录
     */
    @GetMapping("/resume-views")
    public Result<Page<ResumeViewDTO>> getResumeViews(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<ResumeViewDTO> views = personalService.getResumeViews(userId, page, size);
        return Result.success(views);
    }

    /**
     * 职位ID包装类
     */
    @Data
    public static class JobIdWrapper {
        private Long jobId;
    }
} 